// pages/goodsDetail/index.js
import { getGoodsDetail, addCate, getfav, addfav, deletefav } from "../../utils/api"
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        goodsDetail: {},
        datas: "",
        show: false,
        count: 1,
        flag: false
    },
    clickme() {
        this.setData({
            show: true
        })
    },
    addCartImg() {
        addCate({
            goodsId: this.data.goodsDetail.basicInfo.id,
            number: this.data.count,
            token: wx.getStorageSync("token")
        }).then(res => {
            console.log(res);
            // Toast('添加成功');
        })
        this.setData({
            count: 1,
            show: false
        })
    },
    onChange(event) {
        console.log(event.detail);
        this.setData({
            count: event.detail
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        getGoodsDetail({ id: options.id }).then(res => {
            console.log(res);
            getfav({
                goodsId: res.data.basicInfo.id,
                type: res.data.basicInfo.type,
                token: wx.getStorageSync("token")
            }).then(res => {
                console.log(res);
                if (res.data == "未收藏") {
                    this.setData({
                        flag: false
                    })
                } else {
                    this.setData({
                        flag: true
                    })
                }
            })
            this.setData({
                id: options.id,
                goodsDetail: res.data
            })
        })

    },
    change() {
        if (this.data.flag) {
            deletefav({
                type: this.data.goodsDetail.basicInfo.type,
                goodsId: this.data.goodsDetail.basicInfo.id,
                token: wx.getStorageSync("token")
            }).then(res => {
                this.setData({
                    flag: false
                })
            })
        } else {
            addfav({
                type: this.data.goodsDetail.basicInfo.type,
                goodsId: this.data.goodsDetail.basicInfo.id,
                token: wx.getStorageSync("token")
            }).then(res => {
                this.setData({
                    flag: true
                })
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})